package bdcin.servlet.actions.documento;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bdcin.classesbasicas.Area;
import bdcin.classesbasicas.Dissertacao;
import bdcin.classesbasicas.Docente;
import bdcin.classesbasicas.Documento;
import bdcin.classesbasicas.TG;
import bdcin.classesbasicas.Tese;
import bdcin.excecoes.BuscarException;
import bdcin.fachada.BDCInFactory;
import bdcin.fachada.IBDCIn;
import bdcin.servlet.actions.Action;

public class ExibirDocumentoEditarAction extends Action {
	
	@Override
	public void executar(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, BuscarException {
		
		IBDCIn facade = BDCInFactory.getFacade();
		String id = request.getParameter("id");
		
		Documento documento = null;
		try {
			documento = facade.buscarDocumento(Long.valueOf(id));
		
		} catch (Exception e) {
			request.setAttribute("mensagem", e.getMessage());
			request.getRequestDispatcher("mensagem.jsp").forward(request, response);
		}
		List<Area> areas = facade.listarAreas();
		List<Docente> docentes = facade.buscarDocentePorNome("");
		request.setAttribute("documento",documento);		
		request.setAttribute("areas", areas);
		request.setAttribute("docentes", docentes);
				
		
		if(documento instanceof TG){			
			request.getRequestDispatcher("./Controlador?action=tg.TGAction&nomeJSP=alterarTG").forward(request, response);			
		}
		else if (documento instanceof Dissertacao){			
			request.getRequestDispatcher("./Controlador?action=dissertacao.DissertacaoAction&nomeJSP=alterarDissertacao").forward(request, response);		
		}
		else if(documento instanceof Tese){			
			request.getRequestDispatcher("./Controlador?action=tese.TeseAction&nomeJSP=alterarTese").forward(request, response);			
		}
		else{
			request.setAttribute("mensagem", "Este documento ja esta excluido!");
			request.getRequestDispatcher("mensagem.jsp").forward(request, response);
		}

	}
}
